﻿using RightControl.Common;
using RightControl.IRepository;
using RightControl.IService;
using RightControl.Model;
using System.Collections.Generic;
using System.Text;

namespace RightControl.Service
{
    public class TranslationService : BaseService<Translation>, ITranslationService {
        public IProductRepository repository { get; set; }

        public dynamic GetListByFilter(Translation filter, PageInfo pageInfo) {
            string _where = " where 1=1";
            if (!string.IsNullOrEmpty(filter.OriContent)) {
                _where += string.Format(" and OriContent in ({0})", filter.OriContent);
            }
            _where = CreateWhereStr(filter, _where);
            return GetListByFilter(filter, pageInfo, _where);
        }

        public Dictionary<string, Translation> GetWordsTranslation(HashSet<string> words) {
            Translation trans = new Translation();
            StringBuilder wordSb = new StringBuilder();
            foreach (var item in words) {
                wordSb.AppendFormat("'{0}',", item);
            }
            trans.OriContent = wordSb.ToString().TrimEnd(','); // string.Join(",", words);

            dynamic translateRet = GetListByFilter(trans, new PageInfo(1, 10000));
            Dictionary<string, Translation> translateDic = new Dictionary<string, Translation>();
            if (translateRet != null && translateRet.code == 0) {
                for (int i = 0; i < translateRet.count; i++) {
                    translateDic[translateRet.data[i].OriContent] = translateRet.data[i] as Translation;
                }
            }
            return translateDic;
        }
    }
}
